<script lang="ts" setup>
import { ref } from 'vue';
import { useVbenDrawer } from '@vben/common-ui';
import { message } from 'ant-design-vue';
import { useVbenForm } from '#/adapter/form';
import { detailApi, editApi } from '#/api/system/setting';

const key = ref('');

const [Form, formApi] = useVbenForm({
  schema: [
    {
      fieldName: 'gradualBtn',
      label: '按钮颜色渐变',
      component: 'Switch',
      componentProps: {
        class: 'w-auto',
        checkedChildren: '开启',
        checkedValue: 1,
        unCheckedChildren: '关闭',
        unCheckedValue: 0,
      },
      dependencies: {
        show: false,
        // 随意一个字段改变时，都会触发
        triggerFields: [''],
      },
    },
    {
      fieldName: 'mainBg',
      label: '主背景色',
      component: 'Input',
      componentProps: {
        placeholder: '请选择主背景色',
        type: 'color',
      },
    },
    {
      fieldName: 'mainBg2',
      label: '主背景色(渐变)',
      component: 'Input',
      componentProps: {
        placeholder: '请选择主背景色(渐变)',
        type: 'color',
      },
      dependencies: {
        // show(values) {
        //     return values.gradualBtn === 1;
        // },
        // // 随意一个字段改变时，都会触发
        // triggerFields: ['gradualBtn'],
        show: false,
        // 随意一个字段改变时，都会触发
        triggerFields: [''],
      },
    },
    {
      fieldName: 'mainFg',
      label: '主前景色',
      component: 'Input',
      componentProps: {
        placeholder: '请选择主前景色',
        type: 'color',
      },
    },
    {
      fieldName: 'viceBg',
      label: '副背景色',
      component: 'Input',
      componentProps: {
        placeholder: '请选择副背景色',
        type: 'color',
      },
    },
    {
      fieldName: 'viceBg2',
      label: '副背景色(渐变)',
      component: 'Input',
      componentProps: {
        placeholder: '请选择副背景色(渐变)',
        type: 'color',
      },
      dependencies: {
        // show(values) {
        //     return values.gradualBtn === 1;
        // },
        // // 随意一个字段改变时，都会触发
        // triggerFields: ['gradualBtn'],
        show: false,
        // 随意一个字段改变时，都会触发
        triggerFields: [''],
      },
    },
    {
      fieldName: 'viceFg',
      label: '副前景色',
      component: 'Input',
      componentProps: {
        placeholder: '请选择副前景色',
        type: 'color',
      },
    },
    {
      fieldName: 'mainText',
      label: '主文字颜色',
      component: 'Input',
      componentProps: {
        placeholder: '请选择主文字颜色',
        type: 'color',
      },
    },
    {
      fieldName: 'viceText',
      label: '副文字颜色',
      component: 'Input',
      componentProps: {
        placeholder: '请选择副文字颜色',
        type: 'color',
      },
    },
    {
      fieldName: 'pageBg',
      label: '页面背景色',
      component: 'Input',
      componentProps: {
        placeholder: '请选择页面背景色',
        type: 'color',
      },
    },
    {
      fieldName: 'matchColor',
      label: '配色',
      component: 'Input',
      componentProps: {
        placeholder: '请选择配色',
        type: 'color',
      },
    },
  ],
  showDefaultActions: false,
});

const [Drawer, drawerApi] = useVbenDrawer({
  async onConfirm() {
    const { valid } = await formApi.validate();
    if (!valid) return;
    const values = await formApi.getValues();
    drawerApi.lock();
    editApi(key.value, values)
      .then((res) => {
        message.success({
          content: res.message,
        });
        drawerApi.close();
      })
      .catch(() => {
        drawerApi.unlock();
      });
  },
  onOpenChange(isOpen) {
    if (isOpen) {
      const data = drawerApi.getData();
      formApi.resetForm();
      if (data.key) {
        key.value = data.key;
        drawerApi.setState({ title: data.describe })
        detailApi(data.key).then((res) => {
          formApi.setValues(res.values);
        })
      }
    }
  },
  overlayBlur: 1,
  title: '',
  class: 'w-[380px]',
});
</script>
<template>
  <Drawer>
    <Form />
  </Drawer>
</template>
